<?php 
//var_dump($this->rsNhanvien);
?>


<header class="panel-heading">
	<strong> Danh Sách nhân viên </strong>
</header>
<div class="panel-body">
	<div class="adv-table editable-table ">
		<div class="clearfix">
			<div class="btn-group">
				<a class="btn btn-success" data-toggle="modal" href="#addModal">
					Thêm nhân viên <i class="icon-plus"></i>
				</a>
			</div>
			
			<div class="btn-group pull-right">
				<button data-toggle="dropdown" class="btn dropdown-toggle">Xuất dữ liệu <i class="icon-angle-down"></i></button>
				<ul class="dropdown-menu pull-right">
					<li><a href="#">Xem in</a></li>
					<li><a href="#">Xuất PDF</a></li>
					<li><a href="#">Xuất Excel</a></li>
				</ul>
			</div>
		</div>
		<div class="space15"></div>
	  <div role="grid" class="dataTables_wrapper form-inline" id="editable-sample_wrapper">
		
		<table id="editable-sample" class="table table-striped table-hover table-bordered dataTable" aria-describedby="editable-sample_info">
			<thead>
				<tr role="row">
					
					<th class="sorting_disabled" role="columnheader" rowspan="1" colspan="1" style="width: 168px;" aria-label="Username">Họ Tên</th>
					<th class="sorting" role="columnheader" tabindex="0" aria-controls="editable-sample" rowspan="1" colspan="1" style="width: 70px;" aria-label="Full Name: activate to sort column ascending">Ngày Sinh</th>
					<th class="sorting" role="columnheader" tabindex="0" aria-controls="editable-sample" rowspan="1" colspan="1" style="width: 200px;" aria-label="Points: activate to sort column ascending">Địa Chỉ</th>
					<th class="sorting" role="columnheader" tabindex="0" aria-controls="editable-sample" rowspan="1" colspan="1" style="width: 120px;" aria-label="Notes: activate to sort column ascending">Điện Thoại</th>
					<th class="sorting" role="columnheader" tabindex="0" aria-controls="editable-sample" rowspan="1" colspan="1" style="width: 78px;" aria-label="Edit: activate to sort column ascending">Email</th>
					<th class="sorting" role="columnheader" tabindex="0" aria-controls="editable-sample" rowspan="1" colspan="1" style="width: 90px;" aria-label="Delete: activate to sort column ascending">Trạng Thái</th>
					<th class="sorting" role="columnheader" tabindex="0" aria-controls="editable-sample" rowspan="1" colspan="1" style="width: 70px;" aria-label="Delete: activate to sort column ascending"></th>
				</tr>
		  </thead>
		  
		  <tbody role="alert" aria-live="polite" aria-relevant="all">
			  <?php 
			  $stt = 0;
			  foreach($this->rsNhanvien as $rs) { 
				$stt++;
				?>
			  <tr>
				  
				  <td class="  sorting_1"><?php echo $rs['HoTen'] ?></td>
				  <td class=" "> <?php echo date_format(date_create($rs['NgaySinh']), 'd/m/Y') ?></td>
				  <td class=" "> <?php echo $rs['DiaChi'] ?></td>
				  <td class=" "> <?php echo $rs['DienThoai'] ?> </td>
				  <td class=" "> <?php echo $rs['Email'] ?> </td>
				  <td style="text-align: center" class="center"> 
				  	<a href="javascript:void(0)" class="<?php echo ($rs['Trangthai']==1) ? '' : 'red' ?>">
				  		<i class="<?php echo ($rs['Trangthai']==1) ? 'icon-check-sign' : 'icon-lock' ?>"></i>
				  	</a> 
				  </td>
				  <td style="text-align: center">
				  	<a class="ograne edit" data-toggle="modal" href="#addModal" idNhanvien="<?php echo $rs['Id'] ?>"><i class="icon-edit"></i></a> | 
				  	<a class="red xoanhanvien" href="javascript:;" class="delete" data="<?php echo $rs['Id']?>" ><i class="icon-remove"></i></a></td>
			  </tr>
			  <?php } ?>
		</tbody>
		</table>
  </div>
	</div>
</div>
        
		
                              
                              
							  
<!-- Add Modal -->
<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
	  <div class="modal-content">
		  <div class="modal-header">
			  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
			  <h4 class="modal-title">Thêm nhân viên</h4>
		  </div>
		  <div class="modal-body">
	
			  <form name="frmaddNhanvien" id="frmaddNhanvien" class="form-horizontal tasi-form" method="get">
			  	<input type="hidden" name="idNhanvien" id="idNhanvien" value="-1" />
			  	
				<div class="form-group" id="grpHoten">
					<label class="col-sm-2 col-sm-2 control-label">Họ tên</label>
					<div class="col-sm-10">
						<input name="hoten" id="hoten" type="text" class="form-control" />
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-2 control-label">Ngày sinh</label>
					<div class="col-sm-10">
						<input name="ngaysinh" id="ngaysinh" type="text" class="form-control" data-mask="99/99/9999" placeholder="" />
						<span class="help-inline">ngày/tháng/năm</span>
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-2 col-sm-2 control-label">Địa chỉ</label>
					<div class="col-sm-10">
						<input name="diachi" id="diachi" type="text" class="form-control">
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-2 col-sm-2 control-label">Điện thoại</label>
					<div class="col-sm-10">
						<input name="dienthoai" id="dienthoai" type="text" class="form-control">
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-2 col-sm-2 control-label">Email</label>
					<div class="col-sm-10">
						<input name="email" id="email" type="text" class="form-control">
					</div>
				</div>
				<div class="form-group" id="grpquyen">
					<label class="col-sm-2 control-label">Quyền nhân viên</label>
					<div class="col-sm-10">                                          
                         <select class="form-control m-bot15" name = "maquyen" id ="maquyen">
                                 <option value = "-1"> Chọn quyền nhân viên.. </option>
                                 <?php foreach ($this->rsquyennhanvien as $quyennhanvien){?>
                                 	<option value = "<?php echo $quyennhanvien['Id']?>"> <?php echo $quyennhanvien['Ten']?></option>
                                 <?php  }?>
                                 </select>                                          
                    </div>                   
				</div>		
				<div class="form-group">					
					<label class="col-sm-2 col-sm-2 control-label">User</label>
					<div class="col-sm-10">
						<input name="tendangnhap" id="tendangnhap" type="text" disabled class="form-control">
					</div>
				</div>	
				<div class="form-group">
					<label class="col-sm-2 col-sm-2 control-label">Password</label>
					<div class="col-sm-10">
						<input name="matkhau" id="matkhau" type="password" disabled="disabled" class="form-control">
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-2 col-sm-2 control-label">Confirm Password</label>
					<div class="col-sm-10">
						<input name="confirmpass" id="confirmpass" type="password" disabled="disabled" class="form-control">
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-2 col-sm-2 control-label">Ngưng sử dụng</label>
					<div class="col-sm-10">
						<input name="trangthai" type="checkbox" checked="" data-toggle="switch" />
					</div>
				</div>
				
				
			  </form>

		  </div>
		  <div class="modal-footer">
			  <button data-dismiss="modal" class="btn btn-default" type="button">Đóng</button>
			  <button class="btn btn-success" type="button" id="luuthaydoi">Lưu thay đổi</button>
			  <button class="btn btn-success" type="button">Lưu & Thêm mới</button>
		  </div>
	  </div>
  </div>
</div>
<!-- modal -->



<!-- END JAVASCRIPTS -->
<script>
  jQuery(document).ready(function() {
	  EditableTable.init();
	  
	  
	  jQuery("#luuthaydoi").click(function(){
		  
		var values = $('#frmaddNhanvien').serialize();
		
			//alert(values); return;
	  
		  hoten = $('#hoten').val();
		  ngaysinh = $('#ngaysinh').val();
		  diachi = $("#diachi").val();
		  dienthoai = $("#dienthoai").val();
		  email = $("#email").val();
		  tendangnhap=$("#tendangnhap").val();
		  matkhau=$("#matkhau").val();
		  confirmpass=$("#confirmpass").val();
		  maquyen=$("#maquyen").val();

		  if(maquyen != -1)
		  {
			  if(tendangnhap=="" || matkhau=="")
			  {
				  alert("Vui lòng nhập tên đăng nhập và mật khẩu cho nhân viên");
				  return;
			  }
			  if(matkhau != confirmpass)
			  {
				  alert("Mật khẩu nhập lại không trùng khớp");
				  return;
			  }
		  }
		  
		  if(hoten=="")
		  {
			  $('#grpHoten').addClass("has-error");
			  alert("Vui lòng nhập họ tên");
			  $('#hoten').focus();
			  return;
		  }
		  
		  var saveUrl = "<?php echo _BASE_URL?>/quantri/nhanvien/save";
		  var formData = {hoten: hoten, ngaysinh:  "31", diachi :diachi, dienthoai: dienthoai, email: email, tendangnhap:tendangnhap, matkhau:matkhau, maquyen:maquyen}; //Array 

		  var oTable = $('#editable-sample').dataTable();
		  
		  $.ajax({
			  url : saveUrl,
			  type: "POST",
			  //data : formData,
			  data: values,
			  success: function(data, textStatus, jqXHR)
			  {
				  alert("Đã lưu thành công!");
				  location.href= "<?php echo _BASE_URL?>quantri/nhanvien";
				  $(".btn-default").click();
			  },
			  error: function (jqXHR, textStatus, errorThrown)
			  {
				  alert('That bai');
			  }
			});
	  });


	  ///////////////////////////////////////////////////////////
	  // Xoa Nhan vien
	  $(".xoanhanvien").click(function(){

		  if (confirm("Bạn có muốn xóa dòng này không ?") == false) {
              return;
          }
          
		var deleteUrl = "<?php echo _BASE_URL?>/quantri/nhanvien/xoanhanvien";
		var idNhanvien = $(this).attr('data');
		var formData = {id: idNhanvien}; //Array
		
		var tr = $(this).closest('tr');
		
		$.ajax({
			  url : deleteUrl,
			  type: "POST",
			  data : formData,
			  success: function(data, textStatus, jqXHR)
			  {
				  alert('Xóa thành công!');
				  deleteRowofTable(tr);
			  },
			  error: function (jqXHR, textStatus, errorThrown)
			  {
				  alert('Xóa thất bại');
			  }
			});
			
	  });

	  
	  function deleteRowofTable(tr)
	  {
		tr.css("background-color","#FF3700");
			tr.fadeOut(400, function(){
			tr.remove();
		});
	  }


	  ////////////////////////////////////////////////////////////////////
	  /// Sua Nhan Vien
	  $(".edit").click(function(){
		  var idNhanvien = $(this).attr('idNhanvien');
		  var editUrl = "<?php echo _BASE_URL?>quantri/nhanvien/sua/id/" + idNhanvien;
		  
		  $.ajax({
			  url : editUrl,
			  type: "POST",
			  //data : formData,
			  dataType: "text",
			  
			  success: function(data, textStatus, jqXHR)
			  {
				  var Obnhanvien = $.parseJSON(data);
				  $("#idNhanvien").val(idNhanvien);
				  $('#hoten').val(Obnhanvien[0].HoTen);
				  $("#diachi").val(Obnhanvien[0].DiaChi);
				  $("#dienthoai").val(Obnhanvien[0].DienThoai);
				  $("#email").val(Obnhanvien[0].Email);
				  $("#tendangnhap").val(Obnhanvien[0].TenDangNhap);
				  $("#matkhau").val(Obnhanvien[0].MatKhau);
				  $("#maquyen").val(Obnhanvien[0].MaQuyen);

				  var element = (Obnhanvien[0].NgaySinh).split("-");
				  $('#ngaysinh').val(element[2] + '/'+ element[1] +'/' + element[0]);
				  
			  },
			  error: function (jqXHR, textStatus, errorThrown)
			  {
				  alert('Sửa thất bại');
			  }
			});
		  
      });
	  ////////////////////////////////////////
	  //chọn quyền nhân viên
	  jQuery("#maquyen").change(function(){		
		  var Id = $('#maquyen').val();
		  
		  document.frmaddNhanvien.tendangnhap.disabled = false;
		  document.frmaddNhanvien.matkhau.disabled = false;
		  document.frmaddNhanvien.confirmpass.disabled = false;

		  if(Id==-1)
		  {
			  document.frmaddNhanvien.tendangnhap.disabled = true;
			  document.frmaddNhanvien.matkhau.disabled = true;
			  document.frmaddNhanvien.confirmpass.disabled = true;
			  $("#tendangnhap").val("");
			  $("#matkhau").val("");
			  $("#confirmpass").val("");
		  }
		 
	  });	 
	  
  });
</script>